package com.fact.actions;

import java.util.ArrayList;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.fact.dto.MakeDTO;
import com.fact.dto.NodeHierarchyDTO;
import com.fact.dto.ScenarioDTO;
import com.fact.dto.SegmentDTO;
import com.fact.dto.UserDTO;
import com.fact.models.MakeService;
import com.fact.models.NodeHierarchyService;
import com.fact.models.ScenarioService;
import com.fact.models.SegmentService;
import com.fact.utils.AppConstant;

public class NodeHierarchy extends BaseAction implements SessionAware{
	
	private static final long serialVersionUID = 1L;
	private Map<String,Object> session;
	private String page=AppConstant.NODE_HIERARCHY;
	private String pageTitle="FMtool - Node Hierarchy";
	private String opType;
	NodeHierarchyService nodeHierarchyService=new NodeHierarchyService();
	SegmentService segmentService=new  SegmentService();
	MakeService makeService=new MakeService();
	ScenarioService scenarioService=new ScenarioService();
	ArrayList<SegmentDTO> segmentList=new ArrayList<SegmentDTO>();
	ArrayList<NodeHierarchyDTO> nodeHierarchyList=new ArrayList<NodeHierarchyDTO>();
	int segmentId;
	private NodeHierarchyDTO node;
	
	public String nodeHierarchyHome()
	{
		if(opType==null || (!opType.equalsIgnoreCase("save") && !opType.equalsIgnoreCase("delete") && !opType.equalsIgnoreCase("edit")))
			this.session.put("operation_message", null);

		segmentList=segmentService.getSegmentList();
		return SUCCESS;
	}
	public String nodeHierarchyList()
	{
		
		nodeHierarchyList=nodeHierarchyService.getNodeList(segmentId);
		return SUCCESS;
	}
	
	public String newNodeForm()
	{
		segmentList=segmentService.getSegmentList();
		return SUCCESS;
	}
	public String saveNode()
	{
		if(opType==null || (!opType.equalsIgnoreCase("save") && !opType.equalsIgnoreCase("delete") && !opType.equalsIgnoreCase("edit")))
			this.session.put("operation_message", null);
		
		
		segmentList=segmentService.getSegmentList();
		ArrayList<MakeDTO> makeList=makeService.getMakeList();
		ArrayList<ScenarioDTO> scenarioList=scenarioService.getScenarioList();
		
		if(nodeHierarchyService.saveNode(node, makeList, scenarioList)==false)
				{
					addActionMessage("Error in Node Save Operation.");
					return INPUT;
				}
		else
				{
					this.session.put("operation_message", node.getLabel()+" has successfully been added in Node List.");
					node=null;
					return  SUCCESS;
				}
		
	}
	public String deleteNode()
	{
		segmentList=segmentService.getSegmentList();
		if(nodeHierarchyService.deleteNode(node.getNodeId())==false)
		{
			addActionMessage("Error in Node Delete Operation.");
			return INPUT;
			
		}
		else
		{
			this.session.put("operation_message","Node with Node Name '"+ node.getNodeName()+"' has successfully been deleted from Role List.");
			return  SUCCESS;
		}
	
	}
	
	public String editNode()
	{
		segmentList=segmentService.getSegmentList();
		if(nodeHierarchyService.editNode(node)==false)
		{
			addActionMessage("Error in Node Update Operation.");
			return INPUT;
			
		}
		else
		{
			this.session.put("operation_message","Node with Node Name '"+ node.getLabel()+"' has successfully been deleted from Role List.");
			return  SUCCESS;
		}
	}
	
	public String getPage() {
		return page;
	}


	public void setPage(String page) {
		this.page = page;
	}


	public String getPageTitle() {
		return pageTitle;
	}


	public void setPageTitle(String pageTitle) {
		this.pageTitle = pageTitle;
	}


	public String getOpType() {
		return opType;
	}


	public void setOpType(String opType) {
		this.opType = opType;
	}


	public ArrayList<SegmentDTO> getSegmentList() {
		return segmentList;
	}


	public void setSegmentList(ArrayList<SegmentDTO> segmentList) {
		this.segmentList = segmentList;
	}


	public int getSegmentId() {
		return segmentId;
	}
	public void setSegmentId(int segmentId) {
		this.segmentId = segmentId;
	}
	
	
	public NodeHierarchyService getNodeHierarchyService() {
		return nodeHierarchyService;
	}
	public void setNodeHierarchyService(NodeHierarchyService nodeHierarchyService) {
		this.nodeHierarchyService = nodeHierarchyService;
	}
	
	
	public ArrayList<NodeHierarchyDTO> getNodeHierarchyList() {
		return nodeHierarchyList;
	}
	public void setNodeHierarchyList(ArrayList<NodeHierarchyDTO> nodeHierarchyList) {
		this.nodeHierarchyList = nodeHierarchyList;
	}
	
	
	public NodeHierarchyDTO getNode() {
		return node;
	}
	public void setNode(NodeHierarchyDTO node) {
		this.node = node;
	}
	public void setSession(Map<String, Object> session) 
	{
		this.session = session;
	}
	public ServletContext getServletContext()
	{
		return ServletActionContext.getServletContext();
	}

}
